package com.smartnsoft.fractory;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.os.Handler;
import android.widget.Toast;

import com.smartnsoft.droid4me.app.AppPublics;

/**
 * Set the image as Wallpaper.
 * 
 * @author Willy Noel
 * @since 2011.06.17
 */

public final class FractalFeed
{

  private final Activity activity;

  public final Bitmap bitmap;

  public FractalFeed(Activity activity, Bitmap bitmap)
  {
    this.activity = activity;
    this.bitmap = bitmap;
  }



  public void setAsWallpaper(final Handler handler)
  {
    Toast.makeText(activity, R.string.FractalFeed_toast_wallpaperStart, Toast.LENGTH_LONG).show();
    AppPublics.LOW_PRIORITY_THREAD_POOL.execute(activity, new Runnable()
    {
      public void run()
      {
//        final int welcomeWidth = activity.getWallpaperDesiredMinimumWidth() > 0 ? activity.getWallpaperDesiredMinimumWidth()
//            : activity.getWindowManager().getDefaultDisplay().getWidth();
//        final int welcomHeight = activity.getWallpaperDesiredMinimumHeight() > 0 ? activity.getWallpaperDesiredMinimumHeight()
//            : activity.getWindowManager().getDefaultDisplay().getHeight();
   
//                final Bitmap originalBitmap = bitmap;
//                final Bitmap logoBitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.icon);
//                final Bitmap bitmap = BitmapToolbox.mergeBitmaps(originalBitmap, logoBitmap, originalBitmap.getWidth() - logoBitmap.getWidth(),
//                    originalBitmap.getHeight() - logoBitmap.getHeight() - 10);
                try
                {
                  activity.setWallpaper(bitmap);
                  activity.runOnUiThread(new Runnable()
                  {
                    public void run()
                    {
                      Toast.makeText(activity, R.string.FractalFeed_toast_wallpaperSuccess, Toast.LENGTH_SHORT).show();
                    }
                  });
                }
                catch (IOException exception)
                {
                  activity.runOnUiThread(new Runnable()
                  {
                    public void run()
                    {
                      new AlertDialog.Builder(activity).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.problem).setMessage(
                          R.string.FractalFeed_dialog_message_wallpaper).setPositiveButton(android.R.string.ok, null).setCancelable(true).show();
                    }
                  });
                }
                finally
                {
                  // We free the resulting bitmap
                  bitmap.recycle();
                }
              }

      
    });
  }

}
